package club.kingyin.kyblog.core.business.service.web;

import club.kingyin.config.v2.imp.Config;
import club.kingyin.kyblog.core.business.dao.web.*;
import club.kingyin.kyblog.core.business.mapper.web.SiteMapper;
import club.kingyin.kyblog.core.business.vo.web.SiteVo;
import club.kingyin.kyblog.core.utils.serialization.SerializationUtil;
import com.alibaba.fastjson.JSON;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Repository;
import org.springframework.stereotype.Service;

import java.util.ArrayList;

/**
 * @author : kingyins
 * @version : 1.0
 * @program : Ky-Blog
 * @description : 在此处添加类描述
 * @create : 2021年08月12日 17:12
 **/
@Service
@Repository
public class SiteService {

    @Autowired
    private Config config;

    @Autowired
    private SiteMapper siteMapper;

    public SiteVo getSiteInfo() {
        SiteVo vo = new SiteVo();
        vo.setDomain(config.get("domain",String.class));
        vo.setDesc(config.get("desc", String.class));
        vo.setName(config.get("name", String.class));
        vo.setNotice(config.get("notice",String.class));
        vo.setAvatar(config.get("avatar",String.class));
        vo.setSlogan(config.get("slogan", String.class));
        vo.setBackground(config.get("background", String.class));
        return vo;
    }
    public Resources getResources() {
        Resources resources = new Resources();
        resources.setResources(config.get("resources", String.class));
        resources.setAuthor(config.get("author", String.class));
        resources.setIcp(config.get("icp", String.class));
        resources.setTime(config.get("time", String.class));
        return resources;
    }

    public String getAbout() {
        return ConfigService.getAbout(config);
    }

    public Focus[] getFocus() {
        return ConfigService.getFocus(config);
    }

    public Social[] getSocial() {
        return ConfigService.getSocial(config);
    }

    public ArrayList<Friend> getFriends() {
        Friend[] friends = ConfigService.getFriends(config);
        ArrayList<Friend> friendsPublish = new ArrayList<>();
        for (Friend friend : friends) {
            if (friend.getId() < 20) {
                friendsPublish.add(friend);
            }
        }
        return friendsPublish;
    }

    public boolean setSiteInfo(Site site) {
        return siteMapper.updateSiteInfo(site) > 0;
    }

    public boolean addFriend(Friend friend) {
        try {
            Friend[] friends = ConfigService.getFriends(config);
            if (friends.length > 100) {
                return false;
            }
            // 直接修改，保证id大于20
            friend.setId(friends.length < 20 ? 21 + friends.length : friends.length);
            String s = config.get("friends", String.class);
            String t = s.substring(0, s.length() - 1) + ',' + JSON.toJSONString(friend) + ']';
            config.putAndSubmit("friends", t);
            return true;
        } catch (Exception e) {
            return false;
        }

    }
}
